package com.base.thread.demo_29;


public class Demo2 {

	private volatile int signal;
	
	private int get() {
		return signal;
	}
	
	private void set(int signal) {
		this.signal = signal;
	}
	
	public static void main(String[] args) {
		
		Demo2 demo = new Demo2();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (demo) {
					System.out.println(Thread.currentThread().getName()+" 开始执行.");
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					demo.notify();
					System.out.println(Thread.currentThread().getName()+" 执行完了.");
					demo.set(1);
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (demo) {
					System.out.println(Thread.currentThread().getName()+" 开始执行.");
					// 等待signal为1开始执行，否则不能执行
					while (demo.get() != 1) {
						try {
							demo.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					// 当信号为1 的时候，执行代码
					System.out.println(Thread.currentThread().getName()+" 执行完了.");
				}
			}
		}).start();
		
	}
	
}
